﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTH_DTO;
using QLTH_BUSINESS;
using unitlControl;

namespace QuanLy_THPT_S4.Danh_sách
{
    public partial class frmMonHoc : Form
    {
        private readonly clsMonHocBU mhbu;
        private readonly clsMonHocDTO mhdto;
        public frmMonHoc()
        {
            InitializeComponent();
            mhbu = new clsMonHocBU();
            mhdto = new clsMonHocDTO();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmMonHoc_Load(object sender, EventArgs e)
        {
            txtMaMH.Enabled = false;
            gridControl1.DataSource = mhbu.DanhSachMonHoc();

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            mhdto.Tenmh = txtTenMH.Text;
            if (txtMaMH.Text == "" || txtMaMH.Text == null)
            {
                mhdto.Mamh = toancuc.Tao_ma(mhbu.macuoi(), "MH", 2);
                mhbu.ThemMonHoc(mhdto);
                txtTenMH.Text = "";
                txtTenMH.Focus();
                frmMonHoc_Load(sender, e);
            }

        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            mhdto.Mamh = txtMaMH.Text;
            mhdto.Tenmh = txtTenMH.Text;
            mhbu.CapNhatMonHoc(mhdto);
            txtTenMH.Text = "";
            txtTenMH.Focus();
            frmMonHoc_Load(sender, e);
        }

        private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            txtMaMH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAMH").ToString();
            txtTenMH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TENMON").ToString();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageboxUtils.YesNo("Bạn chắc muốn xóa môn học này?") == DialogResult.Yes)
            {
                try
                {
                    mhdto.Mamh = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAMH").ToString();
                    mhbu.XoaMonHoc(mhdto);
                    frmMonHoc_Load(sender, e);
                }
                catch { }
            }
        }

        //private void frmMonHoc_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        btnThem_Click(sender, e);
        //    }
        //}
    }
}
